from re import T
from typing import List
from unittest import result

from sqlalchemy import delete, insert, select, update
from app.api.admin.model.goods.goods import GoodsCateModel, GoodsModel
from app.api.admin.schema.goods.goods import GoodsCateInSchema, GoodsCateOutSchema, GoodsDetailInSchema, GoodsInSchema, GoodsOutSchema
from app.dependencies.database import db


class GoodsService():
    async def list(self):
        return await db.fetch_all(select(GoodsModel))

    async def delete(self, goods_id: int):
        return await db.execute(delete(GoodsModel).where(GoodsModel.id == goods_id))

    async def detail(self, goods_id: int):
        result = await db.fetch_one(select(GoodsModel).where(GoodsModel.id == goods_id))
        return GoodsOutSchema.model_validate(result).model_dump(by_alias=True)

    async def edit(self, goods_id: int, goods_in: GoodsInSchema):
        return await db.execute(update(GoodsModel).where(GoodsModel.id == goods_id).values(goods_in.model_dump()))

    async def add(self, goods_in: GoodsInSchema):
        # print(goods_in.model_dump())
        return await db.execute(insert(GoodsModel).values(goods_in.model_dump()))

    async def cate_add(self, goodsCate_in: GoodsCateInSchema):
        # 插入新数据
        return await db.execute(insert(GoodsCateModel).values(goodsCate_in.model_dump()))

    async def cate_delete(self, goods_cate_id: int):
        return await db.execute(delete(GoodsCateModel).where(GoodsCateModel.id == goods_cate_id))

    async def cate_list(self):
        result = await db.fetch_all(select(GoodsCateModel))

        # print("result", [
        #     GoodsCateOutSchema.model_dump(GoodsCateOutSchema.model_validate(item), by_alias=True) for item in result])
        # goods_cate_list = [
        #     GoodsCateOutSchema.model_dump(GoodsCateOutSchema.model_validate(item), by_alias=True) for item in result]
        return await db.fetch_all(select(GoodsCateModel))

    @ classmethod
    async def instance(cls):
        return cls()
